<?php


namespace App\Dao;


use App\Model\CooperativeUser;
use Hyperf\Database\Model\Builder;

class CooperativeUserDao extends AbstractDao
{
    /**
     * @param int $id
     * @return int|mixed
     */
    public function delete(int $id)
    {
        return $this->where(['id' => $id])->delete();
    }

    public function where(array $where): Builder
    {
        $builder = CooperativeUser::query();
        foreach ($where as $field => $value) {
            switch ($field) {
                case 'ids':
                    $builder->whereIn('id', $value);
                    break;
                case 'keywords':
                    $builder->when(
                        $value,
                        function (Builder $builder) use ($value) {
                            $builder
                                ->where('username', 'like', '%' . $value . '%')
                                ->orwhere('phone', 'like', '%' . $value . '%');
                        }
                    );
                    break;
                default:
                    $builder->where($field, $value);
            }
        }
        return $builder;
    }

    /**
     * 获取指定合作商的所有用户id
     * @param $cooperativeId
     * @return array
     */
    public function getIds($cooperativeId): array
    {
        $result = $this->select(['cooperative_id' => $cooperativeId])->toArray();
        $ids = [];
        if (count($result) > 0) {
            $ids = array($result, 'id');
        }
        return $ids;
    }

    /**
     * 获取合作商客服
     * @param $param
     * @return array
     */
    public function getCustomerList($param)
    {
        return $this->where(
            [
                'cooperative_id'=>$param['cooperative_id'],
                'is_cust' => 1,
                'status' => 0
            ]
        )->select('id','username','avatar')->get()->toArray();
    }
}